<template>
  <div>
    <el-button-group>
      <el-button type="primary" :icon="CirclePlus" v-auth="'CreateForm'" @click="createFormVisible = true">
        新建表单
      </el-button>
      <el-button type="primary" :icon="Refresh" @click="handleQuery">刷新</el-button>
    </el-button-group>
    <el-empty
      v-if="!data.loading && data.resp.total == 0"
      description="开始创建第一张表单"
      @click="createFormVisible = true"
    />
    <el-row :gutter="12" class="list" v-loading="data.loading">
      <el-col :span="6" style="margin-top: 15px" v-for="(item, index) in data.resp.list" :key="index">
        <el-card shadow="hover" class="item">
          <div class="content">
            <el-icon :size="20" color="#409EFF"><Document /></el-icon>
            {{ item.formName }}
          </div>
          <div class="sub">
            <div class="code">编码: {{ item.formCode }}</div>
            <el-divider direction="vertical" />
            <div class="version">版本: V{{ item.version }}</div>
          </div>
          <div class="opera-wrap">
            <div class="opera">
              <el-button type="primary" link :icon="CirclePlus" v-auth="'DesignForm'" @click="handleDesign(item)">
                设计
              </el-button>
              <el-divider direction="vertical" v-if="hasFuncKey('DesignForm') && hasFuncKey('EditForm')" />
              <el-button type="primary" link :icon="Edit" v-auth="'EditForm'" @click="handleOpenEditForm(item)">
                修改
              </el-button>
              <el-divider direction="vertical" v-if="hasFuncKey('DeleteForm') && hasFuncKey('EditForm')" />
              <el-button type="primary" link :icon="Remove" v-auth="'DeleteForm'" @click="handleDelete(item)">
                删除
              </el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <div v-if="data.resp.total > 0" class="page">
      <el-pagination
        v-model:current-page="data.page.pageNum"
        v-model:page-size="data.page.pageSize"
        small
        :page-sizes="[16, 32, 48, 64]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="data.resp.total"
        @size-change="handleQuery"
        @current-change="handleQuery"
      />
    </div>
    <CreateForm ref="refCreateForm" v-model:visible="createFormVisible" @success="handleQuery" />
  </div>
</template>

<script setup lang="ts">
import { ElCard, ElPagination, ElDivider, ElEmpty } from 'element-plus'
import { CirclePlus, Remove, Edit, Refresh, Document } from '@element-plus/icons-vue'
import { useFormDef } from './hook'
import CreateForm from './Create.vue'

const {
  hasFuncKey,
  data,
  handleQuery,
  refCreateForm,
  createFormVisible,
  handleOpenEditForm,
  handleDelete,
  handleDesign
} = useFormDef()
</script>

<style lang="scss" scoped>
:deep(.list) {
  .el-card__body {
    padding-bottom: 0;
    &:hover {
      cursor: pointer;
    }
  }
  .content {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 5px;
    font-size: $--size-font-large;
  }
  .sub {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    color: $--color-text-secondary;
    font-size: $--size-font-small;
  }

  .opera-wrap {
    height: 40px;
  }
  .opera {
    height: 40px;
    line-height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    opacity: 0;
    visibility: hidden;
  }
  .item {
    &:hover {
      .opera {
        visibility: visible;
        opacity: 1;
        transition: opacity 0.4s ease;
      }
    }
  }
}
.page {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>
